<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class PageController extends Controller {
		public function login(){
			$user = SESSION::get("user") ;
			if ( $user!=null && $user->id != "" && $user->email != "" && $user->password != "" ){
				$this->set( "title" , "Home - SMECA " );
				$this->renderLayout("admin");
			}else{
				$this->set( "title" , "Login Page - SMECA " );
				$this->renderDefault("login", "user");
			}
		}
		public function features(){
			$this->renderElement("left_menu");
		}
		public function index(){
			$user = SESSION::get("user") ;
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
		    //print_r( $user );
            //exit();
			if ( $user!=null && $user->id != "" && $user->email != "" && $user->password != "" ){
				if ( $user->type == 1 || $user->type == 2 || $user->type == 3 ){
					Import::AppLib("word");					
					$article = $this->loadModel( "Article" );
					$banner = $this->loadModel( "Banner" );
					
					$menu = $this->loadModel("Menu");
					$menus = $menu->getMenu("Section Menues","en");
					$footer_menus = $menu->getMenu("Section Footer","en");
					
					//$firstMenu = $menus[0];
					//$this->set("selectMenu",$firstMenu->title_en);
					$this->set("selectMenu","");
					$this->set('menus',$menus);
					$this->set('footerMenues',$footer_menus);
					
					$this->set("lastArticles",$article->getLatestArticle() );
					$this->set("topArticles",$article->getLatestArticleByHit() );
					
					/* first request from client */
					$categories = $menu->getCategoryHomepage();//Basic::pr($categories);die();
					$this->set('categories',$categories);
					
					/* second request from client */
					list($homepage_menus,$articles_in_menu) = $menu->getMenuHomepage(5);
					$this->set('homepage_menus',$homepage_menus);
					$this->set('articles_in_menu',$articles_in_menu);
					
					$this->set("topBanners",$banner->topBanners());
					$this->set("rightBanners",$banner->rightBanners());
					$this->set("bottomBanners",$banner->bottomBanners());
					
					$this->set( "title" , "SMECA CLINIC " );
					$this->renderLayout("front");
				}
				else if( $user->type == 0 ){
					$this->set( "title" , "SMECA CLINIC - Admin " );
					$userObj = $this->loadModel("User");
					$this->set("totalRequest",count($userObj->getRequestPremium()));
					$this->renderLayout("admin");
				}
			}else{
				Import::AppLib("word");				
				$article = $this->loadModel("Article");
				$banner = $this->loadModel("Banner");
				
				$menu = $this->loadModel("Menu");
				$menus = $menu->getMenu("Section Menues");//Basic::pr($menus);die();
				$footer_menus = $menu->getMenu("Section Footer");
				$this->set("selectMenu","");
				$this->set('menus',$menus);
				$this->set('footerMenues',$footer_menus);//die(Basic::pr($footer_menus));
				
				$this->set("lastArticles",$article->getLatestArticle());
				$this->set("topArticles",$article->getLatestArticleByHit() );							
				
				/* first request from client */
				$categories = $menu->getCategoryHomepage();//Basic::pr($categories);die();
				$this->set('categories',$categories);
				
				/* second request from client */
				$page = isset($_SESSION['nav']['page-article-by-menu'])?$_SESSION['nav']['page-article-by-menu']:1;
				list($homepage_menus,$articles_in_menu) = $menu->getMenuHomepage(5,$page);
				$this->set('homepage_menus',$homepage_menus);
				$this->set('articles_in_menu',$articles_in_menu);
				
				$this->set("topBanners",$banner->topBanners());
				$this->set("rightBanners",$banner->rightBanners());
				$this->set("bottomBanners",$banner->bottomBanners());
				
				$this->set( "title" , "SMECA CLINIC " );
				$this->renderLayout("front");
			}
		}
		public function detail($menu_id = 0, $cate_id = 0,$article_id = 0){
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
			
			if($article_id){
				Import::AppLib("word");
				$article = $this->loadModel("Article");
				$banner = $this->loadModel("Banner");
				$menu = $this->loadModel("Menu");
				$menus = $menu->getMenu("Section Menues","en");
				$footer_menus = $menu->getMenu("Section Footer","en");
				//die(pr($menus));
				$articles = $article->getLatestArticleByID($article_id);//die(Basic::pr($articles));
				$comments = $article->getComment($article_id);//Basic::pr($comments);die();
				$this->set('article',$articles[0]);
				$this->set('comments',$comments);
				$this->set('menus',$menus);
				$this->set("user_type",true);
				if(SESSION::get('user') != ""){
					$user = SESSION::get('user');
                    if( ($articles[0]->article_type==1) && ($user->type==2) )$this->set("user_type",true);
                    else if( ($articles[0]->article_type==1) && ($user->type!=2) )$this->set("user_type",false);
					$this->set('user_id',$user->id);
				}else {
				    if( ($articles[0]->article_type==1) )$this->set("user_type",false);
				    $this->set('user_id',0);
                }
                
				$this->set("rightBanners",$banner->rightBanners());
				$this->set("rightBanners",$banner->rightBanners());
				$this->set("bottomBanners",$banner->bottomBanners());
				$this->set('footerMenues',$footer_menus);
				$this->set( "title" , "SMECA CLINIC - " . $articles[0] -> article_title );
				$this->renderLayout("frontArticle");
				SESSION::set('tmp_url',$_SERVER['REQUEST_URI']);
			}else{
				$this->redirect( array("com"=>"page","action"=>"index"));
			}
		}
		public function preview($article_id = 0){
			if($article_id){
				Import::AppLib("word");
				$article = $this->loadModel("Article");
				//die(pr($menus));
				$articles = $article->getLatestArticleByID($article_id);//die(Basic::pr($articles));
		
				$this->set('article',$articles[0]);
				
				$this->set( "title" , "SMECA CLINIC - Article Deatil " );
				$this->renderLayout("preview");
				
			}else{
				$this->redirect( array("com"=>"page","action"=>"index"));
			}
		}	
		public function search(){
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
			
			$str = Basic::getVar("str");
			if ( $str ){
				Import::AppLib("word");
				$article = $this->loadModel("Article");
				$banner = $this->loadModel("Banner");
				
				$menu = $this->loadModel("Menu");
				$menus = $menu->getMenu("Section Menues","en");
				$footer_menus = $menu->getMenu("Section Footer","en");
				$this->set("selectMenu","");
				$this->set('menus',$menus);
				$this->set('footerMenues',$footer_menus);
				
				$this->set("search",$str);
				
				//$this->set("topBanners",$banner->topBanners());
				$this->set("rightBanners",$banner->rightBanners());
				$this->set("bottomBanners",$banner->bottomBanners());
				$articles = $article->search( 1 , 20 , $str );//print_r($articles);die();
				$this->set("articles", is_array($articles)&&count($articles)>0 ? $articles : array() );
				
				$this->set( "title" , "SMECA CLINIC - Search " );
				$this->renderLayout("search");
				
			}else{
				$this->index();
			}
		}
		public function sitemap(){
			Import::AppLib("word");
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
			
			$banner = $this->loadModel("Banner");
			
			
			$menu = $this->loadModel("Menu");
			$menus = $menu->getMenu("Section Menues","en");
			$footer_menus = $menu->getMenu("Section Footer","en");
			$all_menus = array_merge($menus,$footer_menus);//die(Basic::pr($all_menus));
			$this->set('all_menus',$all_menus);
			$this->set("selectMenu","");
			$this->set('menus',$menus);
			$this->set('footerMenues',$footer_menus);
			$this->set("rightBanners",$banner->rightBanners());
			$this->set("bottomBanners",$banner->bottomBanners());
			$this->set( "title" , "SMECA CLINIC - Sitemap " );
			$this->renderLayout("sitemap");
		}
		public function addFeedback(){
			if((SESSION::get("user")) == ""){
				SESSION::set('php_uri',$_SERVER['REQUEST_URI']);
				SESSION::set('comment',Basic::getVar('comment'));
				echo "-1";
			}else{
				$user = SESSION::get("user") ;
				$user_id = $user->id;
				$comment = Basic::getVar('comment');
				$aid = Basic::getVar('aid');
				$feedback = $this->loadModel("Feedback");
				echo $feedback -> addFeedback($user_id,$aid,$comment);
			}
		}
		public function contact(){
			Import::AppLib("word");
			if(empty($_POST)){				
				$track = $this->loadModel("TrackVisitor");
				$track -> trackVisitor();
				$visitors = $track->getVisitor();
				$this->set('visitors',$visitors);
					
				$banner = $this->loadModel("Banner");			
					
				$menu = $this->loadModel("Menu");
				$menus = $menu->getMenu("Section Menues");
				$footer_menus = $menu->getMenu("Section Footer");			
				$this->set("selectMenu","");
				$this->set('menus',$menus);
				$this->set('footerMenues',$footer_menus);
				$this->set("rightBanners",$banner->rightBanners());
				$this->set("bottomBanners",$banner->bottomBanners());
				
				$this->set('category', $menu -> getCategoryContactUs());
				$this->set( "title" , "SMECA CLINIC - Contact Us " );
				$this->renderLayout("contact");
			}else{
				$name = $_POST['name'];
				$email = $_POST['email'];
				$message = $_POST['message'];											
				$captcha = Basic::getVar("security_code")!==false?Basic::getVar("security_code"):"";
				$to = 'info@smecaclinic.com';
				if(isEmail($email) == 0){
					$_SESSION['contact']['name'] = $name;
					$_SESSION['contact']['email'] = $email;
					$_SESSION['contact']['message'] = $message;
					$this->redirect(array("com"=>"page","action"=>"contact", "err" => "email"));
					exit(0);
				}
				if($_SESSION["security_code"]!=$captcha){
					$_SESSION['contact']['name'] = $name;
					$_SESSION['contact']['email'] = $email;
					$_SESSION['contact']['message'] = $message;
					$this->redirect(array("com"=>"page","action"=>"contact", "err" => "captcha"));
					exit(0);
				}
				
				require_once(VENDOR_FOLDER . "PHPMailer/class.phpmailer.php");
				 //Import::vendor("PHPMailer/class.phpmailer.php");
					
				$mail = new PHPMailer();
					
				$mail->IsSMTP();
				$mail->Host = "localhost";  // specify main and backup server
					
				$mail->From = $email;
				$mail->FromName = $name;
				$mail->AddAddress($to);
					
				$mail->WordWrap = 50;                                 // set word wrap to 50 characters
				$mail->IsHTML(true);                                  // set email format to HTML
				//$mail->AddEmbeddedImage(BASE_URL . IMG_FOLDER . 'logo.png', 'logoimg', 'logo.png'); // attach file logo.jpg, and later link to it using identfier logoimg
				$mail->Subject = "Contact Us";
				$mail->Body    = $message;
					
				if(!$mail->Send())
				{
				echo "Message could not be sent. <p>";
				echo "Mailer Error: " . $mail->ErrorInfo;
				exit;
				}else{
					unset($_SESSION['contact']);
					$this->redirect(array("com"=>"page","action"=>"contact", "err" => "sucess"));
				}
			}
		}
	}